﻿namespace EasyGo.Utils
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using EasyGo.Models;
    using StackExchange.Profiling;

    [Authorize]
    public abstract class EasyGoJsonController : Controller
    {
        protected override void OnException(ExceptionContext filterContext)
        {
            var response = new JsonResponse();

            var exc = filterContext.Exception;
            while (exc.InnerException != null)
            {
                exc = exc.InnerException;
            }

            response.Fail(exc.Message);
            filterContext.Result = response.ToJsonResult();

            Elmah.ErrorSignal.FromCurrentContext().Raise(filterContext.Exception);
            filterContext.ExceptionHandled = true;
        }

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (!this.ModelState.IsValid)
            {
                throw new Exception("Request is not valid");
            }
        }
    }
}